<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>basen.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_crypt_basen.js.html">basen.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Numeric base conversion library.  Works for arbitrary bases and
<a name="line17"></a> * arbitrary length numbers.
<a name="line18"></a> *
<a name="line19"></a> * For base-64 conversion use base64.js because it is optimized for the specific
<a name="line20"></a> * conversion to base-64 while this module is generic.  Base-64 is defined here
<a name="line21"></a> * mostly for demonstration purpose.
<a name="line22"></a> *
<a name="line23"></a> * TODO: Make base64 and baseN classes that have common interface.  (Perhaps...)
<a name="line24"></a> *
<a name="line25"></a> */
<a name="line26"></a>
<a name="line27"></a>goog.provide(&#39;goog.crypt.baseN&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Base-2, i.e. &#39;01&#39;.
<a name="line32"></a> * @type {string}
<a name="line33"></a> */
<a name="line34"></a>goog.crypt.baseN.BASE_BINARY = &#39;01&#39;;
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Base-8, i.e. &#39;01234567&#39;.
<a name="line39"></a> * @type {string}
<a name="line40"></a> */
<a name="line41"></a>goog.crypt.baseN.BASE_OCTAL = &#39;01234567&#39;;
<a name="line42"></a>
<a name="line43"></a>
<a name="line44"></a>/**
<a name="line45"></a> * Base-10, i.e. &#39;0123456789&#39;.
<a name="line46"></a> * @type {string}
<a name="line47"></a> */
<a name="line48"></a>goog.crypt.baseN.BASE_DECIMAL = &#39;0123456789&#39;;
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Base-16 using lower case, i.e. &#39;0123456789abcdef&#39;.
<a name="line53"></a> * @type {string}
<a name="line54"></a> */
<a name="line55"></a>goog.crypt.baseN.BASE_LOWERCASE_HEXADECIMAL = &#39;0123456789abcdef&#39;;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Base-16 using upper case, i.e. &#39;0123456789ABCDEF&#39;.
<a name="line60"></a> * @type {string}
<a name="line61"></a> */
<a name="line62"></a>goog.crypt.baseN.BASE_UPPERCASE_HEXADECIMAL = &#39;0123456789ABCDEF&#39;;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * The more-known version of the BASE-64 encoding.  Uses + and / characters.
<a name="line67"></a> * @type {string}
<a name="line68"></a> */
<a name="line69"></a>goog.crypt.baseN.BASE_64 =
<a name="line70"></a>    &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&#39;;
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * URL-safe version of the BASE-64 encoding.
<a name="line75"></a> * @type {string}
<a name="line76"></a> */
<a name="line77"></a>goog.crypt.baseN.BASE_64_URL_SAFE =
<a name="line78"></a>    &#39;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_&#39;;
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Converts a number from one numeric base to another.
<a name="line83"></a> *
<a name="line84"></a> * The bases are represented as strings, which list allowed digits.  Each digit
<a name="line85"></a> * should be unique.  The bases can either be user defined, or any of
<a name="line86"></a> * goog.crypt.baseN.BASE_xxx.
<a name="line87"></a> *
<a name="line88"></a> * The number is in human-readable format, most significant digit first, and is
<a name="line89"></a> * a non-negative integer.  Base designators such as $, 0x, d, b or h (at end)
<a name="line90"></a> * will be interpreted as digits, so avoid them.  Leading zeros will be trimmed.
<a name="line91"></a> *
<a name="line92"></a> * Note: for huge bases the result may be inaccurate because of overflowing
<a name="line93"></a> * 64-bit doubles used by JavaScript for integer calculus.  This may happen
<a name="line94"></a> * if the product of the number of digits in the input and output bases comes
<a name="line95"></a> * close to 10^16, which is VERY unlikely (100M digits in each base), but
<a name="line96"></a> * may be possible in the future unicode world.  (Unicode 3.2 has less than 100K
<a name="line97"></a> * characters.  However, it reserves some more, close to 1M.)
<a name="line98"></a> *
<a name="line99"></a> * @param {string} number The number to convert.
<a name="line100"></a> * @param {string} inputBase The numeric base the number is in (all digits).
<a name="line101"></a> * @param {string} outputBase Requested numeric base.
<a name="line102"></a> * @return {string} The converted number.
<a name="line103"></a> */
<a name="line104"></a>goog.crypt.baseN.recodeString = function(number, inputBase, outputBase) {
<a name="line105"></a>  if (outputBase == &#39;&#39;) {
<a name="line106"></a>    throw Error(&#39;Empty output base&#39;);
<a name="line107"></a>  }
<a name="line108"></a>
<a name="line109"></a>  // Check if number is 0 (special case when we don&#39;t want to return &#39;&#39;).
<a name="line110"></a>  var isZero = true;
<a name="line111"></a>  for (var i = 0, n = number.length; i &lt; n; i++) {
<a name="line112"></a>    if (number.charAt(i) != inputBase.charAt(0)) {
<a name="line113"></a>      isZero = false;
<a name="line114"></a>      break;
<a name="line115"></a>    }
<a name="line116"></a>  }
<a name="line117"></a>  if (isZero) {
<a name="line118"></a>    return outputBase.charAt(0);
<a name="line119"></a>  }
<a name="line120"></a>
<a name="line121"></a>  var numberDigits = goog.crypt.baseN.stringToArray_(number, inputBase);
<a name="line122"></a>
<a name="line123"></a>  var inputBaseSize = inputBase.length;
<a name="line124"></a>  var outputBaseSize = outputBase.length;
<a name="line125"></a>
<a name="line126"></a>  // result = 0.
<a name="line127"></a>  var result = [];
<a name="line128"></a>
<a name="line129"></a>  // For all digits of number, starting with the most significant ...
<a name="line130"></a>  for (var i = numberDigits.length - 1; i &gt;= 0; i--) {
<a name="line131"></a>
<a name="line132"></a>    // result *= number.base.
<a name="line133"></a>    var carry = 0;
<a name="line134"></a>    for (var j = 0, n = result.length; j &lt; n; j++) {
<a name="line135"></a>      var digit = result[j];
<a name="line136"></a>      // This may overflow for huge bases.  See function comment.
<a name="line137"></a>      digit = digit * inputBaseSize + carry;
<a name="line138"></a>      if (digit &gt;= outputBaseSize) {
<a name="line139"></a>        var remainder = digit % outputBaseSize;
<a name="line140"></a>        carry = (digit - remainder) / outputBaseSize;
<a name="line141"></a>        digit = remainder;
<a name="line142"></a>      } else {
<a name="line143"></a>        carry = 0;
<a name="line144"></a>      }
<a name="line145"></a>      result[j] = digit;
<a name="line146"></a>    }
<a name="line147"></a>    while (carry) {
<a name="line148"></a>      var remainder = carry % outputBaseSize;
<a name="line149"></a>      result.push(remainder);
<a name="line150"></a>      carry = (carry - remainder) / outputBaseSize;
<a name="line151"></a>    }
<a name="line152"></a>
<a name="line153"></a>    // result += number[i].
<a name="line154"></a>    carry = numberDigits[i];
<a name="line155"></a>    var j = 0;
<a name="line156"></a>    while (carry) {
<a name="line157"></a>      if (j &gt;= result.length) {
<a name="line158"></a>        // Extend result with a leading zero which will be overwritten below.
<a name="line159"></a>        result.push(0);
<a name="line160"></a>      }
<a name="line161"></a>      var digit = result[j];
<a name="line162"></a>      digit += carry;
<a name="line163"></a>      if (digit &gt;= outputBaseSize) {
<a name="line164"></a>        var remainder = digit % outputBaseSize;
<a name="line165"></a>        carry = (digit - remainder) / outputBaseSize;
<a name="line166"></a>        digit = remainder;
<a name="line167"></a>      } else {
<a name="line168"></a>        carry = 0;
<a name="line169"></a>      }
<a name="line170"></a>      result[j] = digit;
<a name="line171"></a>      j++;
<a name="line172"></a>    }
<a name="line173"></a>  }
<a name="line174"></a>
<a name="line175"></a>  return goog.crypt.baseN.arrayToString_(result, outputBase);
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Converts a string representation of a number to an array of digit values.
<a name="line181"></a> *
<a name="line182"></a> * More precisely, the digit values are indices into the number base, which
<a name="line183"></a> * is represented as a string, which can either be user defined or one of the
<a name="line184"></a> * BASE_xxx constants.
<a name="line185"></a> *
<a name="line186"></a> * Throws an Error if the number contains a digit not found in the base.
<a name="line187"></a> *
<a name="line188"></a> * @param {string} number The string to convert, most significant digit first.
<a name="line189"></a> * @param {string} base Digits in the base.
<a name="line190"></a> * @return {Array.&lt;number&gt;} Array of digit values, least significant digit
<a name="line191"></a> *     first.
<a name="line192"></a> * @private
<a name="line193"></a> */
<a name="line194"></a>goog.crypt.baseN.stringToArray_ = function(number, base) {
<a name="line195"></a>  var index = {};
<a name="line196"></a>  for (var i = 0, n = base.length; i &lt; n; i++) {
<a name="line197"></a>    index[base.charAt(i)] = i;
<a name="line198"></a>  }
<a name="line199"></a>  var result = [];
<a name="line200"></a>  for (var i = number.length - 1; i &gt;= 0; i--) {
<a name="line201"></a>    var character = number.charAt(i);
<a name="line202"></a>    var digit = index[character];
<a name="line203"></a>    if (typeof digit == &#39;undefined&#39;) {
<a name="line204"></a>      throw Error(&#39;Number &#39; + number +
<a name="line205"></a>                  &#39; contains a character not found in base &#39; +
<a name="line206"></a>                  base + &#39;, which is &#39; + character);
<a name="line207"></a>    }
<a name="line208"></a>    result.push(digit);
<a name="line209"></a>  }
<a name="line210"></a>  return result;
<a name="line211"></a>};
<a name="line212"></a>
<a name="line213"></a>
<a name="line214"></a>/**
<a name="line215"></a> * Converts an array representation of a number to a string.
<a name="line216"></a> *
<a name="line217"></a> * More precisely, the elements of the input array are indices into the base,
<a name="line218"></a> * which is represented as a string, which can either be user defined or one of
<a name="line219"></a> * the BASE_xxx constants.
<a name="line220"></a> *
<a name="line221"></a> * Throws an Error if the number contains a digit which is outside the range
<a name="line222"></a> * 0 ... base.length - 1.
<a name="line223"></a> *
<a name="line224"></a> * @param {Array.&lt;number&gt;} number Array of digit values, least significant
<a name="line225"></a> *     first.
<a name="line226"></a> * @param {string} base Digits in the base.
<a name="line227"></a> * @return {string} Number as a string, most significant digit first.
<a name="line228"></a> * @private
<a name="line229"></a> */
<a name="line230"></a>goog.crypt.baseN.arrayToString_ = function(number, base) {
<a name="line231"></a>  var n = number.length;
<a name="line232"></a>  var chars = [];
<a name="line233"></a>  var baseSize = base.length;
<a name="line234"></a>  for (var i = n - 1; i &gt;= 0; i--) {
<a name="line235"></a>    var digit = number[i];
<a name="line236"></a>    if (digit &gt;= baseSize || digit &lt; 0) {
<a name="line237"></a>      throw Error(&#39;Number &#39; + number + &#39; contains an invalid digit: &#39; + digit);
<a name="line238"></a>    }
<a name="line239"></a>    chars.push(base.charAt(digit));
<a name="line240"></a>  }
<a name="line241"></a>  return chars.join(&#39;&#39;);
<a name="line242"></a>};
</pre>


</body>
</html>
